$(function(){
	var isflag=$("#isflag").val();
	if(isflag==undefined||isflag==null||isflag==''||isflag==false||isflag=='false'){
		$("#radio1").attr("checked", "checked"); 
	}
	else{
		$("#radio2").attr("checked", "checked"); 
	}
	$("#cancleBut").click(function(){
		parent.$('#dlg').window('close');
		parent.$(document.body).remove()
	});
	var codelength=$('#shopid').width();
	$('#shopid').validatebox({
	    required: true,
	    validType: {
	    	custom_remote:['/shop/checkCode',shopid,'编号已存在']
	    },
	    tipPosition:'top',
	    delay:1000,
	    deltaX:codelength/4
	});
	$('#dealername').combobox({
		 url:'/dealer/getCombobox',
		 valueField:'id',
		 textField:'text',
		 onSelect:function(record){
			 $("#dealerid").val(record.id);
		 }
	});
	$('#statusname').combobox({
		 url:'/shop/getStatusCombobox',
		 valueField:'id',
		 textField:'text',
		 onSelect:function(record){
			 $("#statusid").val(record.id);
		 }
	});
	$('#opendate').datebox({
		 required:true,
		 tipPosition:'top',
		 deltaX:codelength/4
	}); 
	$('#space').validatebox({
		 required:true,
		 tipPosition:'top',
		 validType: 'integer',
		 deltaX:codelength/4
	}); 
	$('#deposit').validatebox({
		 required:true,
		 tipPosition:'top',
		 validType: 'intOrFloat',
		 deltaX:codelength/4
	}); 
	if($("#open").val()!=''){
		var time=Common.formatterDate($("#open").val());
		$('#opendate').datebox('setValue',time);
	}
	$('#resmobile').validatebox({
	    required: true,
	    validType: 'mobile',
	    tipPosition:'top',
	    deltaX:codelength/4
	});
	$('#resperson').validatebox({
	    required: true,
	    tipPosition:'top',
	    deltaX:codelength/4
	});
	$('#shopname').validatebox({
	    required: true,
	    tipPosition:'top',
	    deltaX:codelength/4
	});
	$('#remark').validatebox({
	    required: false,
	    tipPosition:'top',
	    validType: {maxLength:[255]},
	    deltaX:codelength/4
	});
	$("#regionname").combotree({
  		url:'/region/getRegion',
  		id:'id',
  		text:'text',
  		lines:true,
  		animate:true,
  		attributes:'attributes',
  		onBeforeExpand:function(node,param){                         
  			 $('#regionname').combotree("tree").tree("options").url ="/region/getRegion?pid="+node.id;                  
  	    },
		onClick:function(node){
			var n=node;
			var address='';
			var root=$("#regionname").combotree("tree").tree('getRoot',n.target);
			while(1){
				address=n.text+address;
				if(root.id==n.id){
					break;
				}
				n=$("#regionname").combotree("tree").tree('getParent',n.target);
			}
			$("#shopaddress").val(address);
			$("#regionid").val(node.id);
		}
  	});
	//提交表单
	$("#submitBut").click(function(){
		if( $("#dealerid").val()==''|| $("#dealerid").val()==undefined){
			$.messager.alert('失败','经销商不能为空','error');
			return;
		}
		if($("#regionid").val()==''||$("#regionid").val()==undefined){
			$.messager.alert('失败','地区不能为空','error');
			return;
		}
		$("#form").form('submit',{
			url:'/shop/saveShop',
			onSubmit:function(){
		        return $(this).form('validate');
		    },
		    success:function(data){
		    	var jdata=$.parseJSON(data); 
		        if(jdata.code==200){
		        	$.messager.alert('成功','保存成功','info',function(){
		        		parent.$('#dlg').window('close');
		        		window.parent.refreshTab("门店管理");
		        	});
		        }
		        else{
		        	$.messager.alert('失败',jdata.msg,'error');
		        }
		    }
		});
	})
})